package com.cloudfun.campusshare.controller;

import com.cloudfun.campusshare.common.constant.VerifyType;
import com.cloudfun.campusshare.common.response.BaseResult;
import com.cloudfun.campusshare.service.VerifyCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by linyangping on 2019/11/16.
 */
@RestController
@RequestMapping("/v1/code")
@Api(value = "验证码模块", description = "验证码模块", position = 1)
public class VerifyCodeController {

    @Autowired
    private VerifyCodeService verifyCodeService;

    @ApiOperation(value = "发送短信验证码", notes = "发送短信验证码")
    @GetMapping(value = "/phone")
    public BaseResult sendPhoneCode(
            @ApiParam(value = "验证码类型。register：注册；reset：重置；login：登录。", required = true) @RequestParam("type") String type,
            @ApiParam(value = "手机号", required = true) @RequestParam("phone") String phone) throws Exception {
        return verifyCodeService.phoneCodeCreate(VerifyType.valueOf(type.toUpperCase()), phone);
    }

    @ApiOperation(value = "获取短信验证码", notes = "获取短信验证码")
    @Profile("dev")
    @GetMapping
    public BaseResult<String> getPhoneCode(
            @ApiParam(value = "验证码类型，register:注册;", required = true) @RequestParam("type") String type,
            @ApiParam(value = "手机号", required = true) @RequestParam("phone") String phone) throws Exception {
        return verifyCodeService.getPhoneCode(VerifyType.valueOf(type.toUpperCase()), phone);
    }

    @ApiOperation(value = "发送邮箱验证码", notes = "发送邮箱验证码")
    @GetMapping(value = "/email")
    public BaseResult sendEmailCode(@ApiParam(value = "验证码类型", required = true) @RequestParam VerifyType type,
                                    @ApiParam(value = "邮箱", required = true) @RequestParam("email") String email,
                                    HttpServletResponse response, HttpSession session) {
        return verifyCodeService.emailCodeCreate(type, email, response, session);
    }

    @ApiOperation(value = "获取图形验证码", notes = "获取图形验证码")
    @GetMapping(value = "/captcha")
    public void captcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
                        @ApiParam(value = "验证码类型", required = true) @RequestParam("type") VerifyType type) throws Exception {
        verifyCodeService.getCaptcha(httpServletRequest, httpServletResponse, type);
    }

}
